package cn.hdu.liang.ch3;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

class Fruit {
    private String variety = "default";
    private Integer weight = 40;

    public Fruit(Integer weight, String variety) {
        this.weight = weight;
        this.variety = variety;
    }

    public Fruit(Integer weight) {
        this.weight = weight;
    }

    public Integer getWeight() {
        return weight;
    }

    public void setWeight(Integer weight) {
        this.weight = weight;
    }

    public String getVariety() {
        return variety;
    }

    public void setVariety(String variety) {
        this.variety = variety;
    }

    public String toString() {
        return "Fruit{" +
                "variety='" + variety + '\'' +
                ", weight=" + weight +
                '}';
    }
}

public class ConToString {
    static Map<String, Function<Integer, Fruit>> map = new HashMap<>();

    static {
        map.put("apple", Fruit::new);
        map.put("orange", Fruit::new);
    }

    public static void main(String[] args) {
        Fruit fruit = giveMeFruit("apple", 333);
        System.out.println(fruit);
    }

    public static Fruit giveMeFruit(String fruit, Integer weight) {
        return map.get(fruit.toLowerCase()).apply(weight);
    }
}
